//
//  UIImage+Extension.swift
//  Weibo
//
//  Created by Albert Zhu on 16/10/18.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

extension UIImage {
    // 绘制图片以及圆角，避免拉伸和混合，提高性能
    func az_avatarImage(size: CGSize?, backColor: UIColor = UIColor.white, lineColor: UIColor = UIColor.lightGray) -> UIImage? {
        
        var size = size
        
        if size == nil {
            size = self.size
        }
        
        let rect = CGRect(origin: CGPoint(), size: size!)
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        
        backColor.setFill()
        UIRectFill(rect)
        
        let path = UIBezierPath(ovalIn: rect)
        path.addClip()
        
        draw(in: rect)
        
        lineColor.setStroke()
        path.lineWidth = 1
        path.stroke()
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return result
    }
    
    func strenchImage() -> UIImage {
        let image = stretchableImage(withLeftCapWidth: Int(size.width / 2), topCapHeight: Int(size.height / 2))
        
        return image
    }
}
